Skip to content

Printing aligned tables in Go terminal output with uitable

uitable prints rows of values as aligned columns in the terminal. It measures each column’s widest cell, pads the rest to match, and can truncate or wrap long cells. There are no borders or box-drawing characters; if you want those, look at a heavier package such as go-pretty.

The API is small: New(), AddRow(), RightAlign(), and the MaxColWidth, Wrap and Separator fields. The latest tag is v0.0.4 (2019).

Inside a Go module:

Terminal window
go get github.com/gosuri/uitable
package main
import (
"fmt"
"github.com/gosuri/uitable"
)
type item struct {
Name string
Qty int
Price float64
}
func main() {
items := []item{
{"Keyboard", 2, 49.99},
{"Monitor", 1, 189.5},
{"USB-C cable", 10, 7.25},
}
table := uitable.New()
table.Separator = " " // the default is a tab
table.RightAlign(1) // QTY
table.RightAlign(2) // PRICE
table.AddRow("ITEM", "QTY", "PRICE")
for _, it := range items {
table.AddRow(it.Name, it.Qty, fmt.Sprintf("%.2f", it.Price))
}
fmt.Println(table)
}

Output:

ITEM QTY PRICE
Keyboard 2 49.99
Monitor 1 189.50
USB-C cable 10 7.25

Some details:

  • AddRow takes ...interface{} and formats each value with %v. There is no per-column formatter, so format numbers yourself before adding them, as with the price above.
  • RightAlign takes a zero-based column index.
  • *Table implements String(), so fmt.Println(table) works. table.Bytes() gives you a []byte if you’re writing to an io.Writer.
  • The default separator is a tab (uitable.Separator = "\t"), which makes the column gaps depend on your terminal’s tab stops. Setting Separator to spaces gives predictable output.

Set MaxColWidth to cap how wide any column can get. By default, longer cells are cut and end in ...:

table := uitable.New()
table.MaxColWidth = 30
table.AddRow("NAME", "BIO")
table.AddRow("Ada Lovelace", "Mathematician and writer, known for her work on Babbage's Analytical Engine.")
fmt.Println(table)

Set Wrap to true to break long cells across several lines instead. The other cells in that row are padded so the columns stay aligned:

table.Wrap = true

Wrapping works on word boundaries, and it is the better choice for descriptive text; truncation suits IDs and paths where the first part is enough.

uitable only lays out text; it doesn’t sort, page, or draw borders, and it hasn’t seen a release since 2019. For simple aligned output, the standard library’s text/tabwriter does the same job with no dependency.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)